package novel.mzx.com.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.gyf.immersionbar.ImmersionBar;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;

import java.util.List;

import butterknife.BindView;
import novel.mzx.com.R;
import novel.mzx.com.activity.AddCircleActivity;
import novel.mzx.com.activity.CircleDetailsActivity;
import novel.mzx.com.adapter.CircleAdapter;
import novel.mzx.com.base.BaseFragment;
import novel.mzx.com.bean.CircleBean;
import novel.mzx.com.http.Api;
import novel.mzx.com.utils.JSONUtils;
import novel.mzx.com.utils.ViewUtils;


public class ShareFragment extends BaseFragment {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.iv_talk)
    ImageView iv_talk;

    private CircleAdapter circleAdapter;
    private GridLayoutManager mLinearLayoutManager;
    String type;

    @Override
    public int getLayoutResID() {

        return R.layout.fragment_share;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        ImmersionBar.with(this).titleBar(R.id.toolbar).keyboardEnable(true).init();
        ImmersionBar.with(this).keyboardEnable(false).statusBarDarkFont(true, 0.2f).navigationBarColor(R.color.btn3).init();

        mLinearLayoutManager = new GridLayoutManager(getActivity(), 1);
        recyclerView.setLayoutManager(mLinearLayoutManager);
        iv_talk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent2 = new Intent(getActivity(), AddCircleActivity.class);
                startActivity(intent2);
            }
        });
        getDatas();
        registerBoradcastReceiver();
    }

    private void getDatas() {
        ViewUtils.cancelLoadingDialog();
        OkGo.<String>post(Api.mainHost+ Api.listCircleInfoUrl)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Log.e("交流列表",response.body());
                        CircleBean circleBean = JSONUtils.parserObject(response.body(),CircleBean.class);
                        int code = circleBean.getResultCode();
                        if(code == 200){

                            List<CircleBean.Data> data = circleBean.getData();

                            circleAdapter = new CircleAdapter(getActivity(),R.layout.item_circle,data);

                            recyclerView.setAdapter(circleAdapter);

                            circleAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {

                                    Intent intent = new Intent(getActivity(), CircleDetailsActivity.class);
                                    intent.putExtra("id",data.get(position).getJzCircleId()+"");
                                    intent.putExtra("nickName",data.get(position).getJzCircleUsername()+"");
                                    if(null != data.get(position).getJzCircleLogo()){
                                        intent.putExtra("logo",data.get(position).getJzCircleLogo()+"");
                                    }
                                    if(null != data.get(position).getJzCircleFile()){
                                        intent.putExtra("imageUrl",data.get(position).getJzCircleFile()+"");
                                    }

                                    intent.putExtra("content",data.get(position).getJzCircleContent()+"");
                                    intent.putExtra("time",data.get(position).getJzCircleTime()+"");
                                    intent.putExtra("commentSize",data.get(position).getMyComment().size()+"");
                                    intent.putExtra("account",data.get(position).getJzCircleAccount()+"");
                                    intent.putExtra("address",data.get(position).getAddress()+"");
                                    startActivity(intent);


                                }

                                @Override
                                public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                                    return false;
                                }
                            });
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);

                    }
                });
    }

    public void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction("freshCircle");
        //注册广播
        getActivity().registerReceiver(broadcastReceiver, myIntentFilter);
    }


    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            getDatas();
        }
    };






}
